﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

template<class K, class V>
struct BSTreeNode
{
public:
	//节点的构造函数   
	BSTreeNode(const K& key,const V& value)
		:_key(key),
		_value(value),
		_left(nullptr),
		_right(nullptr)
	{}

	K _key;
	V _value;
	BSTreeNode<K,V>* _left;
	BSTreeNode<K,V>* _right;
};

template<class K, class V>
class BSTree
{
	typedef BSTreeNode<K, V> Node;
public:
	//树的构造函数(强制生成默认构造函数  这是防止有了拷贝构造  无法生成对应的默认构造)
	BSTree() = default;

	//插入节点
	bool Insert(const K& key, const V& value)
	{
		if (_root == nullptr)
		{
			_root = new Node(key, value);
			return true;
		}
		
		Node* parent = nullptr;
		Node* cur = _root;

		//找空
		while (cur)
		{
			if (cur->_key <= key)
			{
				parent = cur;
				cur = cur->_right;
			}

			else if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
		}

		cur = new Node(key, value);
		
		if (parent->_key <= key)
		{
			parent->_right = cur;
		}

		else
		{
			parent->_left = cur;
		}

		return true;
	}

	//查找为key的树节点
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key < key)
			{
				cur = cur->_right;
			}

			else if (cur->_key > key)
			{
				cur = cur->_left;
			}

			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	
	//删除为key的节点
	bool Erase(const K& key)
	{
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			//先分三种情况讨论   大于key，小于key，找到了等于key
			if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}

			else if(cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}

			//相等   找到了
			else
			{
			//首先  如果左子树为空(左为空  把右子树给出去)
				if (cur->_left == nullptr)
				{
					//先考虑  要删除的这个节点是否是根节点
					if (cur == _root)
					{
						_root = cur->_right;
					} //这是为根情况的特殊处理

					else
					{
					//左为空  且是parent的左节点
						if (parent->_left == cur)
						{
							parent->_left = cur->_right;
						}
					//左为空  且是parent的右节点
						else
						{
							parent->_right = cur->_right;
						}
					}
					delete cur;
				}

				//右子树为空   
			    else if (cur->_right == nullptr)
				{
					//首先排除根节点情况
					if (cur == _root)
					{
						_root = cur->_left;
					}

					//不为根节点了   
					else
					{
					//右为空   且是根的左节点
						if (parent->_left == cur)
						{
							parent->_left = cur->_left;
						}
					//右为空   且是根的右节点
						else
						{
							parent->_right = cur->_left;
						}
					}
					delete cur;
				}

					//左右子树都不为空
				else
				{
				//找N左子树的值最大结点R(最右结点)或者N右子树的值最⼩结点R(最左结点)替代 N
				//用右子树的最左节点来替代
				//假设这里我们取右子树的最⼩结点作为替代结点去删除 
			//尤其要注意右子树的根就是最⼩情况的情况的处理
				//一定要把cur给replaceParent，否会报错。 

					Node* replaceParent = cur;
					Node* replace = cur -> _right;

					while (replace->_left)
					{
						//进循环就需要更新    向左更新
						replaceParent = replace;
						replace = replace->_left;
					}

					//出了循环左就为nullptr了    更新cur的key   
					cur->_key = replace->_key;
					//如果  replaceParent的左节点为替代节点   就把替代节点的右给上replaceParent的左
					if (replaceParent->_left == replace)
						replaceParent->_left = replace->_right;    //防止根节点被删除的情况   所以最初的replaceParent不能给空指针
					//如果  replaceParent的右节点为替代节点   就把替代节点的右给replaceParent的右
					else
						replaceParent->_right = replace->_right;

					delete replace;
				}
				return true;
			}
		}
		return false;
	}

	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	
	//中序遍历的子函数
	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_key << "  " << root->_value << endl;
		_InOrder(root->_right);
	}
	
private:
	Node* _root = nullptr;
};


void TestBSTree()
{
	BSTree<string, string> dict;
	dict.Insert("insert", "插入");
	dict.Insert("erase", "删除");
	dict.Insert("left", "左边");
	dict.Insert("string", "字符串");

	string str;
	while (cin >> str)
	{
		auto ret = dict.Find(str);
		if (ret)
		{
			cout << str << ":" << ret->_value << endl;
		}
		else
		{
			cout << "单词拼写错误" << endl;
		}
	}

	string strs[] = { "苹果", "西瓜", "苹果", "樱桃", "苹果", "樱桃", "苹果", "樱桃", "苹果" };
	// 统计水果出现的次数
	BSTree<string, int> countTree;
	for (auto str : strs)
	{
		auto ret = countTree.Find(str);
		if (ret == NULL)
		{
			countTree.Insert(str, 1);
		}
		else
		{
			ret->_value++;
		}
	}
	countTree.InOrder();
}

int main()
{
	TestBSTree();

	return 0;
}